/**
 * @tag controllers, home
 * Displays a table of <%= plural %>.  Lets the user 
 * ["<%=name%>Controller.prototype.form submit" create], 
 * ["<%=name%>Controller.prototype.&#46;edit click" edit],
 * or ["<%=name%>Controller.prototype.&#46;destroy click" destroy] <%= plural %>.
 */
jQuery.Controller.extend('<%=name%>Controller',
/* @Static */
{
    onDocument: true
},
/* @Prototype */
{
    /**
     * When the page loads, gets all <%= plural %> to be displayed.
     */
    load: function(){
        if(!$("#<%= underscore %>").length) 
            $(document.body).append($(document.createElement('div')).attr('id','<%= underscore %>'))
        <%= name %>.findAll({}, this.callback('list'));
    },
    /**
     * Displays a list of <%= plural %> and the submit form.
     * @param {Array} <%= plural %> An array of <%=name%> objects.
     */
    list: function(<%= plural %>){
        $('#<%= underscore %>').html(this.view('init', {<%= plural %>:<%= plural %>} ))
    },
    /**
     * Responds to the create form being submitted by creating a new <%=name%>.
     * @param {jQuery} el A jQuery wrapped element.
     * @param {Event} ev A jQuery event whose default action is prevented.
     */
    "form submit" : function(el, ev){
        ev.preventDefault();
        new <%= name %>( el.formParams() ).save();
    },
    /**
     * Listens for <%= plural %> being created.  When a <%= underscore %> is created, displays the new <%= underscore %>.
     * @param {String} called The open ajax event that was called.
     * @param {Event} <%= underscore %> The new <%= underscore %>.
     */
    "<%= underscore %>.created subscribe": function(called, <%= underscore %>){
		$("#<%= underscore %> tbody").append( this.view("list", {<%= plural %>:[<%= underscore %>]}) )
        jQuery("#<%= underscore %> form input[type!=submit]").val(""); //clear old vals
    },
    /**
     * Creates and places the edit interface.
     * @param {jQuery} el The <%= underscore %>'s edit link element.
     */
    '.edit click' : function(el){
        var <%= underscore %> = el.parents().model();
        $( "."+<%= underscore %>.identity() ).html(this.view('edit', <%= underscore %>))
    },
    /**
     * Removes the edit interface.
     * @param {jQuery} el The <%= underscore %>'s cancel link element.
     */
    '.cancel click': function(el){
        this.show(el.parents().model());
    },
    /**
     * Updates the <%= underscore %> from the edit values.
     */
    '.update click': function(el){
        var <%= underscore %>El = el.parents('.<%= underscore %>'); 
        <%= underscore %>El.model().update( <%= underscore %>El.formParams()  )
    },
    /**
     * Listens for updated <%= plural %>.  When a <%= underscore %> is updated, 
     * update's its display.
     */
    '<%= underscore %>.updated subscribe' : function(called, <%= underscore %>){
        this.show(<%= underscore %>);
    },
    /**
     * Shows a <%= underscore %>'s information.
     */
    show: function(<%= underscore %>){
        $("."+<%= underscore %>.identity()).html(this.view('show',<%= underscore %>))
    },
    /**
     *  Handle's clicking on a <%= underscore %>'s destroy link.
     */
    '.destroy click' : function(el){
        if(confirm("Are you sure you want to destroy?"))
            el.parents().model().destroy();
    },
    /**
     *  Listens for <%= plural %> being destroyed and removes them from being displayed.
     */
    "<%= underscore %>.destroyed subscribe" : function(called, <%= underscore %>){
        <%= underscore %>.elements().remove();  //removes ALL elements
    }
});